<?php
/*
 DevXplorer - Devana's F1le Expl0rer
 Auth0r = i putu yoga permana
 version 0.1 alpha

 */
include("libs.php");
$page = new page();
$file = new file();

if(!isset($_SESSION["devxplorer"])) {
	login();
}
if(isset($_GET["do"])) {
	$do = explode(":", $_GET["do"]);

	switch($do[0]) {
		case "logout": logout(); break;
	}
} else {
	main();
}

function main() {
	global $page;

	if(isset($_GET["dir"])) {
		$folder = str_replace(".", "", $_GET["dir"]); //Prevent Inclus|0n
		$dir_p = "../".$folder."/";
	} else {
		$folder = "";
		$dir_p = "../";
	}
	if(isset($_GET["edit"])) {

		edit($dir_p,$_GET["edit"]);
	}
	$path = $dir_p;
	$dir = dir($path);
	$page->content = "<table id='list'><tr id='head'><td>FileName</td><td>FileInfo</td><td>Do ?</td></tr>";

	$i = 0;
	$fext = array("php", "css", "html", "js", "sql", "dat", "zip");
	$iext = array("jpg", "jpeg", "gif", "png", "bmp");
	if(isset($_GET["page"])) {
		$pagenum = $_GET["page"]-1;
	} else {
		$pagenum = 0;
	}
	while(($entry = $dir->read()) !== false) {
		if($entry != "." && $entry != "..") {
			if(($i >= ($pagenum * 40)) && ($i <= (($pagenum + 1) * 40))) {
				//BgColor Different
				if(($i % 2) == 0) {
					$color = "#ffffff";
				} else {
					$color = "#7696ff";
				}
				//Img Icon
				$ext = pathinfo($entry);
				if(in_array($ext["extension"], $fext)) {
					$link = "<a href='?dir=". $_GET["dir"] ."&edit=$entry'>Edit</a>";
					$info = strtoupper($ext["extension"])." File";
					$img = "text.gif";
				} elseif(is_dir($path.$entry)) {
					$link = "<a href='?dir=$dir_p$entry'>Enter</a>";
					$info = "Directory";
					$img = "folder.gif";
				} else {
					$link = "--";
					$info = "Unknown File";
					$img = "generic.gif";
				}
				//0utput
				$page->content .= "<tr style='background-color: $color;'><td><img src=\"icons/$img\">$entry</td><td>$info</td><td>$link</td></tr>\n";

			}
			$i++;
		}
	}
	$page->content .= "</table><br/>";
	$link = "Page :: ";
	for($a = 1; $a <= ceil($i/40); $a++) {
		if($a == $pagenum+1) {
			$link .= "<font class='red'>$a</font> |";
		} else {
			$link .= "<a href='?dir=$dir_p&page=$a'>$a</a> |";
		}
	}

	$page->title = "DevXplorer - Devana's Files Explorer <br/>$link";
	$page->show();
}

function login() {
	global $page;
	include("config.php");
	session_start();
	$page->content .= "
		<p>Input Password To Access DevXplorer. You can't enter until you input right pass.
		<form action='". $_SERVER["PHP_SELF"] ."' method='post'>
		<input type='password' name='input' value=''><br/>
		<input type='submit' name='submit' value='LoGin!'>
		</form>
	";
	if(isset($_POST["submit"])) {
		if($_POST["input"] == $password) {
			$_SESSION["devxplorer"] = true;
			die(header("Location: ./"));
		} else {
			$page->content .= "<div class='red'>Wr0ng P4ssw0rd !</div>";
		}
	}
	$page->title = "DevXplorer - Devana's File Explorer";
	$page->show();
}

function logout() {
	global $page;
	$_SESSION = array();
	session_destroy();
	die(header("Location: ./"));
}

function edit($dir,$f) {
	global $page, $file;

	$file->path = $dir.$f;
	$content = htmlspecialchars($file->get());

	if(isset($_POST["save"])) {
		$content = stripslashes($_POST["content"]);

		$result = $file->write($content);

		if($result) {
			$r = "<p style='color: green'>Success Save</p>";
		} else {
			$r = "<p style='color: red'>Success Save</p>";
		}
	} else {
		$r = "";
	}
	$page->content .= "
	<form action=\"". $_SERVER["REQUEST_URI"]. "\" method=\"post\">
	<textarea cols=\"70\" name=\"content\" rows=\"20\">$content</textarea><br/><br/>
	<input type=\"submit\" name=\"save\" value=\"Save\">
	<input type=\"reset\" name=\"reset\" value=\"Reset\"> <a href=\"./?dir=$dir\"><button>go back</button></a>
	</form><br/><br/>$r
	";

	$page->title = "DevEdit - Devana's Source Code Editor";
	$page->show();

}
?>